// import complex_query from '@dfeidao/atom-nodejs/db/complex-query';
import query from '@dfeidao/atom-nodejs/db/query';
import log from '@dfeidao/atom-nodejs/logger/log';
import { IncomingHttpHeaders } from 'http';
import { productid, systemid } from '../../atom/config';

interface Message {
	// cookie: {
	// 	uk: string;
	// 	[key: string]: string
	// };
	// urls: {
	// 	base: string;
	// 	origin: string;
	// 	url: string;
	// };
	// query: {};
	// params: {};
	// headers: {};
	// captcha: string;
	page: {
		no: number;
		value_no: string;
		// value_title: string;
	};
}

interface IWebResult {
	data: unknown;
	cookie?: {
		[name: string]: string;
	} | null;
	content_type?: string;
	headers?: {
		[key: string]: string;
	};
	attachment?: string;
	redirect?: string;
	status_code?: number;
}

export default async function atom(message: Message, action_id: string, session_id: string, headers: IncomingHttpHeaders): Promise<IWebResult> {
	log('Service begin path:e:\feidao\KaoHe\kh\src\basic-data-wzz\zj-000002\s002,action_id:' + action_id);

	/*	const q1 = complex_query(action_id, session_id, systemid);
		const res1 = await q1
			.add_field('basic_data', 'value_no', 'value_no')
			.add_field('basic_data', 'value_title', 'value_title')
			.add_field('property_optional_values', 'value_no', 'value_no')// 值标题
			.add_field('property_optional_values', 'priority', 'priority')// 排序码
			.add_field('property_optional_values', 'optional_value', 'optional_value')// 可选值
			.add_field('property_optional_values', 'value_description', 'value_description')// 可选值描述
			.add_field('property_optional_values', 'optional_value_no', 'optional_value_no')// 可选值编号
			.inner_join('basic_data', 'property_optional_values', ['value_no', 'value_no'])
			.exec();
	*/
	const q1 = query(action_id, session_id, systemid);
	q1.prepare('property_optional_values', ['value_no', 'priority', 'optional_value', 'value_description', 'optional_value_no'], { productid, value_no: { $like: '%' + message.page.value_no + '%' } }, 200, message.page.no, [], []);
	// const [res1] = await q1.exec();

	const [data] = await q1.exec();
	const res1 = data.map((value) => {
		return { ...value, v: JSON.stringify(value) };
	});
	log('Service end path:e:\feidao\KaoHe\kh\src\basic-data-wzz\zj-000002\s002,action_id:' + action_id);
	return {
		data: res1
	};
}
